#include <math.h>
#include "report.h"
#include "rkvector.h"
#include "tabular.h"

#define NUMSAMPLES 1000000

int main(int argc, char ** argv)
{
  int i;
  int cnt[3];
  TabularCPD_t tab;

  TabularCPD_Init_Params(&tab, 6, RkVector_alloc(3, .8, .15, .05),
                         RkVector_alloc(3, .75, .14, .11), 
                         RkVector_alloc(3, .6, .24, .16),
                         RkVector_alloc(3, .57, .31, .12),
                         RkVector_alloc(3, .53, .36, .11),
                         RkVector_alloc(3, .5, .27, .23));

  TabularCPD_Init_Args(&tab, 2, 3, 2);

  if (TabularCPD_Prob(&tab, 1, 2, 1, 1) != .31)
    return 1;

  if (TabularCPD_LogProb(&tab, 0, 2, 2, 0) != log(.53))
    return 2;

  cnt[0] = cnt[1] = cnt[2] = 0;
  for (i=0; i<NUMSAMPLES; i++)
    cnt[TabularCPD_Sample(&tab, 2, 0, 1)] ++;
  
  if ((fabs(((double)cnt[0]/NUMSAMPLES) - .75) > 1e-3)
      || (fabs(((double)cnt[1]/NUMSAMPLES) - .14) > 1e-3)
      || (fabs(((double)cnt[2]/NUMSAMPLES) - .11) > 1e-3))
    return 3;
  
  return 0;
}
